package self.hoy.demo.grainmall.member.controller;

import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import self.hoy.demo.common.exception.BizCodeEnum;
import self.hoy.demo.common.to.MemberTo;
import self.hoy.demo.common.to.auth.WeiboUser;
import self.hoy.demo.grainmall.member.entity.Member;
import self.hoy.demo.grainmall.member.exception.PhoneExistException;
import self.hoy.demo.grainmall.member.exception.UserNameExistException;
import self.hoy.demo.grainmall.member.service.MemberService;
import self.hoy.demo.common.utils.PageUtils;
import self.hoy.demo.common.utils.R;
import self.hoy.demo.grainmall.member.vo.MemberLoginVo;
import self.hoy.demo.grainmall.member.vo.MemberRegisterVo;


/**
 * 会员
 *
 * @author Hoy
 * @email xxx@gmail.com
 * @date 2020-07-04 22:40:35
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		Member member = memberService.getById(id);
        if (member != null) {
            member.setPassword(null);
        }
        return R.ok().put("member", member);
    }

    /**
     * 获取会员信息
     * @param mobile
     * @return
     */
    @GetMapping("/information")
    public MemberTo getMemberByMobile(@RequestParam("mobile") String mobile) {
//        return         memberService.getById();
        return null;
    }

    /**
     * 检查会员是否唯一
     * @param field 可选值 e: email u: username p:phone number
     * @param text 用户名或者邮箱地址或者手机号码
     * @return ok() 表示唯一（可以添加）； error()表示已存在（不能新增）
     */
    @PostMapping("/checkUnique")
    public R checkUnique(@RequestParam("field") String field, @RequestParam("text") String text) {
        boolean isUnique = true;
        if ("e".equals(field)) {
            isUnique = memberService.isEmailUnique(text);
        } else if ("u".equals(field)) {
            isUnique = memberService.isUserNameUnique(text);
        } else if ("p".equals(field)) {
            isUnique = memberService.isPhoneNumberUnique(text);
        }
        return isUnique ? R.ok() : R.error();
    }

    /**
     * 会员注册
     * @param vo
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody MemberRegisterVo vo) {
        try {
            memberService.register(vo);
        } catch (UserNameExistException e) {
            return R.error(BizCodeEnum.USER_EXIST);
        } catch (PhoneExistException e) {
            return R.error(BizCodeEnum.PHONE_EXIST);
        }
        return R.ok();
    }

    /**
     * 会员登录
     * @param vo
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo vo) {
        Member entity = memberService.login(vo);
        if (entity == null) {
            return R.error(BizCodeEnum.WRONG_LOGIN);
        } else {
            entity.setPassword(null);
            return R.ok().put("data", entity);
        }
    }


    /**
     * 会员登录
     * @param vo
     * @return
     */
    @PostMapping("/oauth/login")
    public R oauthLogin(@RequestBody WeiboUser vo) {
        Member entity = memberService.login(vo);
        if (entity == null) {
            return R.error(BizCodeEnum.WRONG_LOGIN);
        } else {
            entity.setPassword(null);
            return R.ok().put("data", entity);
        }
    }

    /**
     * 保存
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody Member member){
		memberService.save(member);

        return R.ok();
    }
     */

    /**
     * 修改
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody Member member){
		memberService.updateById(member);

        return R.ok();
    }
     */

    /**
     * 删除
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
     */

}
